package com.ts.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

public class MethodUtils {
	
	
	
	/**
	 * 通用接口访问
	 * 返回接口数据：json格式
	 * @author zbb
	 */
	public static String urlConnect(String getUrl) throws Exception{
		StringBuffer jsonStr=new StringBuffer();
		if(StringUtils.isNotBlank(getUrl)){
			URL url=new URL(getUrl);
			HttpURLConnection con=(HttpURLConnection)url.openConnection();
			con.setRequestMethod("GET");
			con.setRequestProperty("type", "submit");
			con.setDoOutput(true);
			con.connect();
			BufferedReader r=new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
			String line="";
			while((line=r.readLine())!=null){
				jsonStr.append(line);
			}
			r.close();
			con.disconnect();
		}
		return jsonStr.toString();
	}

	public static String formatSortType(String allSortType){
		String lockStr="";
		String unlockStr="";
		if(StringUtils.isNotBlank(allSortType)){
			String[] arr=allSortType.split(";");
			if(arr!=null&&arr.length>0){
				for(int i=0;i<arr.length;i++){
					String typ=arr[i];
					if(StringUtils.isNotBlank(typ)){
						String[] arrc=typ.split(",");
						if(arrc!=null&&arrc.length>0){
							String n=arrc[0];//排序字段
							String s=arrc[1];//排序类型
							String l=arrc[2];//锁定类型
							if("lock".equals(l)){//锁定字段优先排序
								if("up_sort".equals(s)){
									lockStr+=n+" asc,";
								}else if("down_sort".equals(s)){
									lockStr+=n+" desc, ";
								}
							}else{
								if("up_sort".equals(s)){
									unlockStr+=n+" asc, ";
								}else if("down_sort".equals(s)){
									unlockStr+=n+" desc, ";
								}
							}
						}
					}
				}
			}
		}
		return lockStr+unlockStr;
	}
	/**
	 * 组织查询排序slq
	 * 参数格式如下：
	 * 	countnum,desc,unlock|transno,desc,unlock|gjlx,asc,unlock|destination,asc,unlock|startings,asc,unlock|
	 * @param allSortType
	 * @return
	 */
	public static String querySort(String allSortType){
		String lockStr="";
		String unlockStr="";
		String finalSort="";
		System.out.println(allSortType);
		if(StringUtils.isNotBlank(allSortType)){
			String[] arr=allSortType.split("\\|");
			if(arr!=null&&arr.length>0){
				for(int i=0;i<arr.length;i++){
					String typ=arr[i];
					System.out.println(typ+"---");
					if(StringUtils.isNotBlank(typ)){
						String[] arrc=typ.split(",");
						if(arrc!=null&&arrc.length>0){
							String n=arrc[0];//排序字段
							String s=arrc[1];//排序类型
							String l=arrc[2];//锁定类型
							if("lock".equals(l)){//锁定字段优先排序
								if("asc".equals(s)){
									lockStr+=n+" asc,";
								}else if("desc".equals(s)){
									lockStr+=n+" desc, ";
								}
							}else{
								if("asc".equals(s)){
									unlockStr+=n+" asc, ";
								}else if("desc".equals(s)){
									unlockStr+=n+" desc, ";
								}
							}
						}
					}
				}
				finalSort=lockStr+unlockStr;
				//System.out.println(finalSort.substring(1,finalSort.length()-1));
				//finalSort=finalSort.substring(1,finalSort.length()-1);
			}
		}
		return finalSort;
	}
	
	
	/**
	 * uuid字符串
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	/**
	 * 将给的参数，根据flag判断，转换为带引号的字符串（针对mybatis中statementType="STATEMENT"情况下的参数赋值）
	 * @param page_param
	 * @param paramValue
	 * @param flag
	 * @throws Exception
	 */
	public static void switchParams(Map<String,Object> page_param,String paramName,String paramValue, boolean flag) throws Exception{
		if(StringUtils.isNotBlank(paramValue)){
			if(page_param==null){
				page_param=new HashMap<String,Object>();
			}
			if(flag==true){
				page_param.put(paramName, "'"+paramValue+"'");
			}else{
				page_param.put(paramName, paramValue);
			}
		}
	}
	/**
	 * 为空处理1:为空则置为"";
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String ifnull(Object obj) throws Exception{
		String r_obj="";
		if(obj!=null){
			r_obj=obj.toString().trim();
		}
		return r_obj;
	}
	/**
	 * 为空处理2:为空则置为replaceStr
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String ifnull(Object obj,String replaceStr) throws Exception{
		String r_obj="";
		if(obj!=null){
			r_obj=obj.toString().trim();
		}else{
			r_obj=replaceStr;
		}
		return r_obj;
	}
	
}
